package com.happy_lab_management.demo.common.labSchedule.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happy_lab_management.demo.admin.labSchedule.service.LabScheduleService;
import com.happy_lab_management.demo.admin.user.service.TeacherService;
import com.happy_lab_management.demo.common.labSchedule.entity.FormSchedule;
import com.happy_lab_management.demo.common.labType.service.LabTypeService;
import com.happy_lab_management.demo.common.section.service.SectionService;
import com.happy_lab_management.demo.common.weekday.service.WeekdayService;
import com.happy_lab_management.demo.system.service.SystemService;
import com.happy_lab_management.demo.technician.lab.service.LabService;
import com.happy_lab_management.entity.*;
import com.happy_lab_management.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/common/schedule")
@RestController("commonSchedule")
@Slf4j
public class LabScheduleController {
    @Autowired
    private LabScheduleService labScheduleService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private LabTypeService labTypeService;
    @Autowired
    private WeekdayService weekdayService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private LabService labService;
    @Autowired
    private SectionService sectionService;

    @RequestMapping("/list")
    public Result<List<LabSchedule>> list(){
        String key="current_semester_id";
        Integer semesterId= systemService.getValueByKey(key);
        QueryWrapper<LabSchedule> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("semester_id",semesterId);
        List<LabSchedule> labSchedules = labScheduleService.list(queryWrapper);
        return Result.success(labSchedules);
    }

    /**
     * 查询接口，限定返回的格式和顺序
     *  {
     *         weekday:"星期一",
     *         weekRowSpan:4,
     *       labType:'软件实验室',
     *         labTypeRowSpan:3,
     *         labNumber:'732',
     *         //所有的schedule_x都需要重新发送请求查询
     *         scheduleA:"课程：汇编语言\n教师：张三\n班级：21级软件工程\n周次：5-12",
     *         scheduleB:"课程：汇编语言\n教师：张三\n班级：21级软件工程\n周次：5-12",
     *    }
     * @return
     */
    @RequestMapping("/getFormSchedule")
    public Result<List<FormSchedule>> getFormSchedule(){
        //获取当前学期
        Integer currentSemesterId = systemService.getValueByKey("current_semester_id");
        Section currentSectionDict = sectionService.getById(currentSemesterId);

        //获取所有的weekday：从星期一到星期五
        List<Weekday> weekdayList = weekdayService.list();
        //获取所有的session（1-2，3-4xxxx）
        List<Section> sectionList = sectionService.list();
        //获取所有的LabType
        List<LabType> labTypeList = labTypeService.list();
        //新建这个为返回的东西，顺序很重要
        List<FormSchedule> formScheduleList = new ArrayList<>(weekdayList.size()*labTypeList.size()*18);
        //循环遍历所有的LabType
        int weekdayIndexBefore=0;
        int weekdayIndexAfter=0;
        int labTypeIndexBefore=0;
        int labTypeIndexAfter=0;
        FormSchedule weekdayFormSchedule = null;
        for(Weekday weekday:weekdayList){
            weekdayIndexBefore=weekdayIndexAfter;
            for(LabType labTypeItem:labTypeList){
                labTypeIndexBefore=labTypeIndexAfter;
                //根据labType查询所有的lab
                QueryWrapper<Lab> labQueryWrapper = new QueryWrapper<>();
                labQueryWrapper.eq("lab_type",labTypeItem.getId());
                List<Lab> labList = labService.list(labQueryWrapper);
                //现在需要查询出lab实验室，所有星期的课程
                for(Lab labItem:labList){
                    FormSchedule formScheduleItem = new FormSchedule();
                    if(weekdayIndexAfter==weekdayIndexBefore) {
                        weekdayFormSchedule=formScheduleItem;
                    }
                    //除了第一行,其他的全部-默认为0
                    formScheduleItem.setWeekRowSpan(0);
                    if(labTypeIndexAfter==labTypeIndexBefore){
                        //这时候已经有该labType有多少实验室了
                        formScheduleItem.setLabTypeRowSpan(labList.size());
                    }else{
                        formScheduleItem.setLabTypeRowSpan(0);
                    }
                    weekdayIndexAfter++;
                    labTypeIndexAfter++;
                    //放入信息
                    //将当前学期信息放入
                    formScheduleItem.setSemesterId(currentSemesterId);
                    formScheduleItem.setSemesterDict(currentSectionDict.getSectionName());
                    //设置labType信息
                    formScheduleItem.setLabType(labTypeItem.getId());
                    formScheduleItem.setLabTypeDict(labTypeItem.getLabTypeName());
                    //将lab:labItem的信息添加进去
                    formScheduleItem.setLabId(labItem.getId());
                    formScheduleItem.setLabNumber(labItem.getLabNumber());
                    //将weekday信息写进去
                    formScheduleItem.setWeekday(weekday.getId());
                    formScheduleItem.setWeekdayDict(weekday.getWeekdayName());

                    //遍历查询所有的课表,构造查询条件:currentSemesterId/weekday/labItem(labType不一定要带)
                    QueryWrapper<LabSchedule> labScheduleQueryWrapper = new QueryWrapper<>();
                    labScheduleQueryWrapper.eq("semester_Id",currentSemesterId);
                    labScheduleQueryWrapper.eq("weekday",weekday.getId());
                    labScheduleQueryWrapper.eq("lab_id",labItem.getId());
                    //查询到该学期该周,该实验室的所有课程
                    List<LabSchedule> labScheduleList = labScheduleService.list(labScheduleQueryWrapper);
                    for(LabSchedule labScheduleItem:labScheduleList){
                        //teacherName需要预处理
                        Teacher scheduleTeacher = teacherService.getById(labScheduleItem.getTeacherName());
                        String teacherName=scheduleTeacher!=null?scheduleTeacher.getName():"老师";
                        String message=String.format("课程:%s\n教师:%s\n班级:%s\n周次:%s\n",
                                labScheduleItem.getCourseName(),
                                teacherName,
                                labScheduleItem.getClassname(),
                                labScheduleItem.getStartWeek()+"-"+labScheduleItem.getEndWeek());
                        switch (labScheduleItem.getSection()){
                            case 1:
                                formScheduleItem.setScheduleA(message);
                                break;
                            case 2:
                                formScheduleItem.setScheduleB(message);
                                break;
                            case 3:
                                formScheduleItem.setScheduleC(message);
                                break;
                            case 4:
                                formScheduleItem.setScheduleD(message);
                                break;
                            case 5:
                                formScheduleItem.setScheduleE(message);
                                break;
                            case 6:
                                formScheduleItem.setScheduleF(message);
                                break;
                        }
                    }
                    //将该数据放回返回的值中
                    formScheduleList.add(formScheduleItem);
                }
            }
            //清点完成所有信息,那么此时的week就知道有什么多少行了
            if(weekdayFormSchedule!=null)
                weekdayFormSchedule.setWeekRowSpan((weekdayIndexAfter-weekdayIndexBefore));
        }

        return Result.success(formScheduleList);

    }
    @RequestMapping("/getLabType")
    public Result<List<String>> getLabType(){
        List<LabType> labTypes = labTypeService.list();
        List<String> stringList=new ArrayList<>();
        labTypes.forEach(labType -> {
            stringList.add(labType.getLabTypeName());
        });
        return Result.success(stringList);
    }
}
